import { Injectable } from '@nestjs/common';
import * as csv from 'csv-parser';
import * as fs from 'fs';

@Injectable()
export class WarningService {
  private csvsDir: string;
  private fwcheckResults: any[];

  constructor() {
    this.csvsDir = '/tmp/flashlight/csvs';
    this.loadDevices();
  }

  private async loadDevices() {
    this.fwcheckResults = await new Promise<any[]>((resolve, reject) => {
      const results: any[] = [];
      fs.createReadStream(`${this.csvsDir}/WARNINGS_FW_CHECK.csv`)
        .pipe(csv())
        .on('data', (row: any) => {
          results.push(row);
        })
        .on('end', () => {
          resolve(results);
        })
        .on('error', (error) => {
          reject(error);
        });
    });
  }

  getAll() {
    return this.fwcheckResults;
  }
}
